<?php
namespace app\index\controller;
use Des3\DES3;
use think\Controller;
use think\Exception;
use think\Loader;
use think\View;
use WxpayAPI\WxPayNotify as WxWebPayNotify;

class Pay extends Controller
{
    /**
     * 获取微信支付数据
     * @return \think\response\Json
     * @throws \Exception
     */
    public function wx_pay() {
        $member_id = session('member_id');
        $model = input("param.model");
        $user_integral = input("param.user_integral");
        $order_id = input("param.order_id");
        if ($model == 'order_zb') {
            $param = [
                'price' => input("param.price", 0),
                'num' => input("param.num", 0),
                'openid' => session('openid'),
                'nickname' => session('nickname'),
                'headimgurl' => session('headimgurl'),
                'video_id' => input("param.video_id", 0)
            ];
            $order = local_invoke_api('create_zb_order', $param);
            if ($order['code'] != 200) {
                throw new \Exception($order['msg']);
            }
            $order = $order['data'];

            $price = $order->price;
            //$price = 0.01;
        } else {
            $order = model($model)->get(['id' => $order_id, 'member_id' => $member_id]);
            if (!$order) {
                throw new \Exception("订单不存在");
            }
            $price = $order->price;
            if ($model == 'activity' && $order->product_id > 0) {
                // 如果是活动并且有赞助商品,支付金额需要加上商品金额
                $price += $order->product->price;
            }
            // 测试阶段  支付金额设为0.01
//            $price = 0.01;
        }

        // 如果是用了积分抵扣，计算剩余待支付金额
        if ($user_integral == 1) {
            $clothes_money = $order->orderMember->clothes_money_recharge + $order->orderMember->clothes_money_donate;
            $price = $clothes_money > $order->price ? 0 : $order->price - $clothes_money;
        }

        /**
         * 微信支付附加数据
         */
        $des3 = new DES3();
        $attach['sign'] = $des3->encrypt('jxtx.com');

        // 售后单支付时获取售后单号
        $order_sn = $order['order_sn'];

        $view = new View();
        $wx_data = \app\api\service\Pay::wx_web_pay($order_sn, $price, json_encode($attach));
        if ($wx_data['code'] != 200) {
            return json($wx_data);
        }
        $view->assign('order_id', $order_id);
        $view->assign('model', $model);
        $view->assign('order_price', $price);
        $view->assign('jsApiParameters', $wx_data['jsApiParameters']);
        $wxpay_html = $view->fetch();
        return json(array('code' => 200,'tpl' => $wxpay_html));
    }

    /**
     * 微信支付回调页面
     */
    public function notify()
    {
        $data = $GLOBALS['HTTP_RAW_POST_DATA'];
        $notify = new WxWebPayNotify();
        $notify->Handle(false);
    }

    /**
    * 微信支付回调页面
     */
    public function app_notify() {
        $data = $GLOBALS['HTTP_RAW_POST_DATA'];
        require_once EXTEND_PATH . 'WxpayAPPAPI/WxPayNotify.php';
//            Loader::import('WxpayAPPAPI.WxPayNotify');
        $notify = new \WxPayNotify();
        $notify->Handle(false);
    }

    /**
     * 支付宝回调方法
     */
    public function ali_notify() {
        $data = input('param.');
        file_put_contents(RUNTIME_PATH . 'ali_notify.txt', json($data));
    }

    /**
     * 检查安全密码是否正确
     * @return \think\response\Json
     */
    public function check_safe_password() {
        $safe_password = input('param.safe_password');
        $des = new DES3();
        $param = [
            'member_id' => session('member_id'),
            'safe_password' => $des->encrypt($safe_password)
        ];
        $data = local_invoke_api('check_safe_password', $param);
        return json($data);
    }

    /**
     * 检查安全密码是否设置
     * @return \think\response\Json
     */
    public function check_is_set_safe_password() {
        $param = [
            'member_id' => session('member_id')
        ];
        $data = local_invoke_api('is_set_safe_password', $param);
        return json($data);
    }

    /**
     * 站内支付
     * @return \think\response\Json
     */
    public function instation_pay() {
        $param = input('param.');
        $param['member_id'] = session('member_id');
        $data = local_invoke_api('instation_pay', $param);
        return json($data);
    }
}
